from numpy.distutils.core import setup
from numpy.distutils.extension import Extension


import sys
from os.path import join

try:
    import numpy
except ImportError:
    print "numpy must be installed to build pywcsgrid."
    print "ABORTING."
    sys.exit(1)

try:
    numpy_include = numpy.get_include()
except AttributeError:
    numpy_include = numpy.get_numpy_include()



from numpy.distutils.misc_util import Configuration



import os.path



TPMDIR = './coords/tpm'

def get_tpm_sources(TPMDIR):
    import glob
    tpm_sources = glob.glob(os.path.join(TPMDIR, "*.c"))
    return tpm_sources


def main():

    WCSLIBDIR="./wcslib-4.3.1/"

    setup(name = "pywcsgrid2",
          version = "0.1b1",
          description = "",
          author = "Jae-Joon Lee",
          maintainer_email = "lee.j.joon@gmail.com",
          license = "BSD",
          platforms = ["Linux","Mac OS X"], 
          packages = ['pywcsgrid2', 'pywcsgrid2.coords'],
          package_dir={'pywcsgrid2':'lib',
                       'pywcsgrid2.coords':'coords/lib'},

          libraries=[["pgsbox",
                      dict(sources=[WCSLIBDIR+"pgsbox/"+s for s in ["pgsbox.f","cpgsbox.c", "pgwcsl.c", "pgcrfn.f"]],
                           include_dirs=[WCSLIBDIR, WCSLIBDIR+'C',
                                         WCSLIBDIR+'pgsbox']
                           )],
                     
                     ["tpm",
                      dict(sources=get_tpm_sources(TPMDIR),
                           include_dirs=[TPMDIR]
                           )],
                     ],
          

          ext_modules=[ Extension("pywcsgrid2._wcsgrid",
                                  ["src/_wcsgrid.pyx"],
                                  language="f77",
                                  include_dirs=[WCSLIBDIR,
                                                WCSLIBDIR+'src',
                                                WCSLIBDIR+'C',
                                                WCSLIBDIR+'pgsbox',
                                                "src",
                                                numpy_include,
                                                ],
                                  library_dirs=[WCSLIBDIR+'C',], # "../pgsbox"
                                  libraries=["pgsbox", "wcs-4.3"], #, "pgnumb" , "coordconv"
                                  #libraries=["pgsbox"], #, "pgnumb" , "coordconv"
                                  ),

                        Extension("pywcsgrid2.coords._pytpm",
                                  ["coords/src/_pytpm.pyx",
                                   "coords/src/coord_conv.c",
                                   #"coords/src/blackbox.c",
                                   ],
                                  include_dirs=['./coords',
                                                './coords/src',
                                                numpy_include,
                                                ],
                                  libraries=["tpm"],
                                  ),
                        ],
          )
    

if __name__ == "__main__":
    main()
